/*
 * Copyright (C) 2007-2010 Siemens AG
 *
 * This program and its interfaces are free software;
 * you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */

package com.siemens.ct.exi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import com.siemens.ct.exi.CodingMode;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.siemens.ct.exi.EXIFactory;
import com.siemens.ct.exi.GrammarFactory;
import com.siemens.ct.exi.api.sax.EXIResult;
import com.siemens.ct.exi.grammar.Grammar;
import com.siemens.ct.exi.helpers.DefaultEXIFactory;

public final class XML2EXIS {

	static final String OUTPUT_FOLDER = "./sample-out/";
	static final String EXI_EXTENSION = "_exi";
	static final String EXI_SCHEMA_EXTENSION = "_exi_schema";
	static final String XML_EXTENSION = ".xml";
	static int NUMBER_OF_RUNS = 1;

	// XML
	String xmlLocation;
	String xmlName;
	// XML Schema
	String xsdLocation;
	
    boolean bit_align = false;    // 0: byte alignment (default)
                                  // 1: bit alignment
    
    boolean strict_mode = false;

    // final setting of alignment and strict mode in "protected void codeSchemaInformed()"

	private void parseAndProofFileLocations(String[] args)
	{
		if (args.length == 0) {

			// XML file location

			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/1.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/2.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/3.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/4.xml";
			
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/5.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/6.xml";
			
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/7.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/8.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/9.xml";
//			xmlLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/10.xml";
			
			File xmlFile = new File(xmlLocation);
			xmlName = xmlFile.getName();

			
			// XML Schema file location
	
			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/1_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/2_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/3_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/4_soap.xsd";
			
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/5_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/6_soap.xsd";
			
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/7_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/8_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/9_soap.xsd";
//			xsdLocation = "/home/user/workspace/uexi-exificient/sample-data/DPWS/10_soap.xsd";
				
			File xsdFile = new File(xsdLocation);
			

			if (xmlFile.exists() && xsdFile.exists())
			{
				File outputDir = new File(OUTPUT_FOLDER);
				outputDir.mkdirs();
				return;
			}
			
		}
	}

	private String getEXILocation(boolean schemaLess) {
		if (schemaLess) {
			return OUTPUT_FOLDER + xmlName + EXI_EXTENSION;
		} else {
			return OUTPUT_FOLDER + xmlName + EXI_SCHEMA_EXTENSION;
		}
	}

	protected void codeSchemaLess() throws Exception {
		String exiLocation = getEXILocation(true);		

		// encode
		OutputStream exiOS = new FileOutputStream(exiLocation);
		SAXResult exiResult = new EXIResult(exiOS);
		encode(exiResult.getHandler());
		exiOS.close();

		// decode
		//SAXSource exiSource = new EXISource();
		//XMLReader exiReader = exiSource.getXMLReader();
		//decode(exiReader, exiLocation);
	}

	protected void codeSchemaInformed() throws Exception {
		String exiLocation = getEXILocation(false);

		// create default factory and EXI grammar for schema
		EXIFactory exiFactory = DefaultEXIFactory.newInstance();

		GrammarFactory grammarFactory = GrammarFactory.newInstance();


        strict_mode = true;		// strict mode on / off
        bit_align = false;		// bit or byte alignment


        if (strict_mode == true)
        {
            exiFactory.getFidelityOptions().setFidelity("STRICT", true); // set strict mode in FidelityOptions
            grammarFactory.set_strict_mode(strict_mode);                 // inform GrammarFactory
        }

		Grammar g = grammarFactory.createGrammar(xsdLocation);
		exiFactory.setGrammar(g);

		if (bit_align == true)  exiFactory.setCodingMode(CodingMode.BIT_PACKED);
		if (bit_align == false) exiFactory.setCodingMode(CodingMode.BYTE_PACKED);

		// encode
		OutputStream exiOS = new FileOutputStream(exiLocation);
		EXIResult saxResult = new EXIResult(exiOS, exiFactory);
		encode(saxResult.getHandler());
		exiOS.close();

		// decode
		//EXISource saxSource = new EXISource(exiFactory);
		//XMLReader xmlReader = saxSource.getXMLReader();
		//decode(xmlReader, exiLocation);
	}

	protected void encode(ContentHandler ch) throws SAXException, IOException {
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(ch);

		// parse xml file
		xmlReader.parse(new InputSource(xmlLocation));
	}

	protected void decode(XMLReader exiReader, String exiLocation)
			throws SAXException, IOException, TransformerException {

		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();

		InputStream exiIS = new FileInputStream(exiLocation);
		SAXSource exiSource = new SAXSource(new InputSource(exiIS));
		exiSource.setXMLReader(exiReader);

		OutputStream os = new FileOutputStream(exiLocation + XML_EXTENSION);
		transformer.transform(exiSource, new StreamResult(os));
		os.close();
	}

	public static void main(String[] args) throws Exception {
		/*
		 * Note: we are using default coding options and SAX
		 */
		
		if (args.length == 0) {

			XML2EXIS sample = new XML2EXIS();
			sample.parseAndProofFileLocations(args);			
			// schema-less
			//for (int i = 0; i < NUMBER_OF_RUNS; i++) {
			//	sample.codeSchemaLess();
			//	System.out.print(". ");
			//}
			//System.out.println();
			//System.out.println("# SchemaLess - " + NUMBER_OF_RUNS + " run(s)");
			//System.out.println("\t" + " --> " + sample.getEXILocation(true));
			//System.out.println("\t" + " <-- " + sample.getEXILocation(true) + XML_EXTENSION);

						
			// schema-informed
			for (int i = 0; i < NUMBER_OF_RUNS; i++) {
				sample.codeSchemaInformed();
				System.out.print(". ");
			}
			System.out.println();
			System.out.println("# SchemaInformed - " + NUMBER_OF_RUNS + " run(s)");
			System.out.println("\t" + " --> " + sample.getEXILocation(false));
			
			//System.out.println("\t" + " <-- " + sample.getEXILocation(false) + XML_EXTENSION);

		} 
		else {
			System.out.println("# EXIficient Sample, input files specified");
			System.out.println("# Currently not supported");
			//System.out.println("Usage: " + XML2EXIS.class.getName() + " xmlFile xsdFile [Everything for BIT_Packed]");
		}
	}

}
